sample() function to create the numbers.
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
fancy_matrix <-
sample(1:1000, 8*8, replace = TRUE) %>%
matrix(nrow = 8, ncol = 8)
fancy_matrix
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 352 324 608 115 1000 848 268 674
## [2,] 866 30 508 462 236 41 162 956
## [3,] 350 77 544 969 548 571 960 550
## [4,] 50 345 871 710 162 530 121 204
## [5,] 869 129 244 61 774 550 291 462
## [6,] 538 682 847 168 110 294 114 677
## [7,] 493 481 72 643 148 814 572 752
## [8,] 137 385 807 71 669 634 837 297
raster() function can be fed with matrices to create a raster layer.
library(raster)
## Loading required package: sp
##
## Attaching package: 'raster'
## The following object is masked from 'package:dplyr':
##
## select
fancy_raster_layer <-
raster::raster(fancy_matrix)
raster::plot(fancy_raster_layer)
The raster() function can not only be used to create raster data on the fly, which is also not very interesting. Instead, we can use it to import already prepared data.
.tiff files in the ./data/ folder of the workshop directory (repository). Do you get any warning messages?
getwd(). Setting is done with setwd().
immigrants_cologne <-
raster::raster("../data/immigrants_cologne.tif")
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded datum Unknown based on GRS80 ellipsoid in CRS definition
# The warning message is due to the current transition of CRS definition
# systems. You can ignore it in this course.
+ and - operators.
# load all layers
immigrants_cologne <-
raster::raster("../data/immigrants_cologne.tif")
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded datum Unknown based on GRS80 ellipsoid in CRS definition
germans_cologne <-
raster::raster("../data/germans_cologne.tif")
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded datum Unknown based on GRS80 ellipsoid in CRS definition
inhabitants_cologne <-
raster::raster("../data/inhabitants_cologne.tif")
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded datum Unknown based on GRS80 ellipsoid in CRS definition
# create sum layer
immigrants_germans_sum <-
immigrants_cologne + germans_cologne
# create difference layer
difference_layer <-
inhabitants_cologne - immigrants_germans_sum
difference_layer
## class : RasterLayer
## dimensions : 289, 264, 76296 (nrow, ncol, ncell)
## resolution : 100, 100 (x, y)
## extent : 4094850, 4121250, 3084050, 3112950 (xmin, xmax, ymin, ymax)
## crs : +proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m +no_defs
## source : memory
## names : layer
## values : -2, 0 (min, max)
# get a summary statistic
summary(difference_layer)
## layer
## Min. -2
## 1st Qu. 0
## Median 0
## 3rd Qu. 0
## Max. 0
## NA's 62522
# create a table of counts
difference_layer %>%
as.data.frame() %>%
table()
## .
## -2 -1 0
## 11 2052 11711